import 'package:flutter/material.dart';
import 'package:flutterlib/common/constants/constants.dart';
import 'package:flutterlib/common/net/dao_result.dart';
import 'package:flutterlib/common/net/retrofit/interest_api.dart';
import 'package:flutterlib/common/utils/navigator_utils.dart';
import 'package:flutterlib/model/interest/interest_bean.dart';
import 'package:flutterlib/pages/interest/list/interest_list_widget_page.dart';
import 'package:flutterlib/widgets/bussiness/post/interest_list_item.dart';
import 'package:flutterlib/widgets/pull/nested/simple_pull_load_manger.dart';
import 'package:thrio/thrio.dart';
import 'package:flutterlib/common/utils/utils.dart';

///type = 0 获取我的或者是传入userid的兴趣
///type = 1 获取全部兴趣
class InterestNormalListWidgetPage extends InterestListWidgetPage {
  final int userId;
  final int type;
  final List<InterestBean> interests;

  InterestNormalListWidgetPage({this.interests, this.type : 0, Key key, this.userId}) : super(key: key);

  @override
  InterestSimpleListWidgetPageState createState() =>
      InterestSimpleListWidgetPageState();
}

///页面 KeepAlive ，同时支持 动画Ticker
class InterestSimpleListWidgetPageState
    extends InterestListWidgetPageState<InterestNormalListWidgetPage> {
  final SettingsStore store =
  Provider.of<SettingsStore>(currentContext, listen: false);


  @override
  void initState() {
    super.initState();
    manger = SimplePullLoadManger(
        getDataLogic: getDataLogic,
        renderItem: renderItem,
        enablePullDown: false,
        enablePullUp: false,
        renderEmpty: renderEmptyView,
    );
  }

  Widget renderEmptyView(BuildContext context) {
    return new Container(
      height: 350.h,
      alignment: Alignment.center,
      child: new Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Container(
            child: Text("数据为空", style: QuhuiConstant.normalText),
          ),
        ],
      ),
    );
  }

  getDataLogic(int page) async {
    print(page);

    if(widget.type == 0){
      if(currentUserId == 0){
        return new DataResult(<InterestBean>[], true);
      }
      if((widget.interests??const []).length > 0){
        return new DataResult(widget.interests, true);
      }
      var interests = await InterestApi.builder()
          .getMyInterests(widget.userId ?? currentUserId);
      return new DataResult(interests, true);
    }else{
      var interests = await InterestApi.builder()
          .getInterests();
      trueDebugPrint("flutterting" + "flutterting allinterests" + interests?.length.toString());
      return new DataResult(interests, true);
    }
  }

  Widget renderItem(index, interest) {
    //提交
    // var post = pullLoadWidgetControl.dataList[index];
    return InterestListItem(interest, onPressed: () {
      NavigatorUtils.gotoInterestDetail(context, interest.id);
    });
  }
}
